package com.spzx.product.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.api.domain.vo.CategoryVo;
import com.spzx.product.domain.Category;
import com.spzx.product.helper.CategoryHelper;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    /**
     * 获取分类下拉树列表
     *
     * @return
     */
    @Override
    public List<Category> treeSelect(Long parentId) {
        //先把父类ID查出来
        List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, parentId));

        //判断是否有子节点
        if (!CollectionUtils.isEmpty(categoryList)) {
            for (Category category : categoryList) {
                Long count = categoryMapper.selectCount(new LambdaQueryWrapper<Category>()
                        .eq(Category::getParentId, category.getId()));
                //有子节点的把hasChildren属性设为true
                category.setHasChildren(count > 0);
            }
        }
        return categoryList;
    }

    /**
     * 根据子id获取父类及其以上id
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Long> getAllCategoryIdList(Long categoryId) {
        ArrayList<Long> idList = new ArrayList<>();//存放正序id
        //存储顺序：逆袭
        List<Category> categoryList = this.getParentCategoryList(categoryId, new ArrayList<Category>());
        Collections.reverse(categoryList);
        for (int i = 0; i < categoryList.size(); i++) {
            Category category = categoryList.get(i);
            idList.add(category.getId());
        }
        return idList;
    }

    /**
     * 递归：根据本级分类id,查询本级分类及所有上级分类数据；存储，逆序
     * 1.三级找二级、二级找一级 过程是同样过程
     * 2.一级找父亲没有了就结束了。
     *
     * @param categoryId
     * @param categoryList
     * @return
     */
    private List<Category> getParentCategoryList(Long categoryId, ArrayList<Category> categoryList) {
        while (categoryId > 0) {
            //查找本级
            Category category = categoryMapper.selectOne(new LambdaQueryWrapper<Category>().eq(Category::getId, categoryId));
            //放入集合中
            categoryList.add(category);
            //递归调用本方法
            return getParentCategoryList(category.getParentId(), categoryList);
        }
        return categoryList;
    }


    //=====================前端调用的方法==============================

    /**
     * 查询所有一级分类
     *
     * @return
     */
    @Override
    public List<CategoryVo> getOneCategory() {
        List<Category> allCategoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>().eq(Category::getParentId, 0));
        return allCategoryList.stream().map(item -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
    }

    /**
     * 分类页的树结构
     *
     * @return
     */
    @Override
    public List<CategoryVo> tree() {
        List<CategoryVo> categoryVoList = new ArrayList<>();
        //查询出所有分类
        List<Category> categoryAllList = categoryMapper.selectList(null);//全部703条数据
        //进行类转换
        if (!CollectionUtils.isEmpty(categoryAllList)) {//判断返回的数据是否为空
            for (Category category : categoryAllList) {
                CategoryVo vo = new CategoryVo();
                BeanUtils.copyProperties(category, vo);
                categoryVoList.add(vo);
            }
        }
        //构建树结构，组装父子关系
        List<CategoryVo> treeVoList = CategoryHelper.buildTree(categoryVoList);
        //返回组装好的父子关系集合
        return treeVoList;
    }
}
